<?php

namespace App\Http\Controllers\Home;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginControllers extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home/index/login');
    }

    //登录验证
    public function store(Request $request)
    {
        $rules = ['captcha' => 'required|captcha'];
        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
            return redirect('login')->with('errCaptcha', '验证码错误');
        }else{
            $uname = $request->input('loginname');
            $pwd = ($request->input('loginpwd'));
            $login = new User;
            $spwd = $login->where('uname',$uname)->get();
            if(!empty($spwd[0])){
                if(Hash::check($pwd,$spwd[0]->password)){
                    $request->session()->put('name',$uname);
                    return redirect('/');
                }
            }
            return redirect('login')->with('message','账号或密码错误');
        }

    }
}
